/**
 * Created by jiangxiaowei on 2016/12/22.
 */

'use strict';
let mongoose = require('mongoose');
let VersionHistorySchema = new mongoose.Schema({
  'name': String,
  'version': String,
  'username': String,
  'email': String,
  'branch': String,
  'publishTime': String
});

//静态方法供model使用
VersionHistorySchema.statics = {

  getHistoryCount: function({name}, cb) {
    return this.find({name}).count().exec(cb);
  },

  getLastHistory: function({name,index}, cb) {
    return this.aggregate([{'$match':{name}}, {'$limit':index}, {'$sort':{'_id':-1}}, {'$limit':-1}]).exec(cb);
  },

  getHistoriesByPage: function({name, pageSize, objectId}, cb) {
    return this.find({name,'_id':{'$gt': objectId}}).sort({'_id': -1}).limit(pageSize).exec(cb);
  },

  getHistoriesBySkip: function({name, pageNum, pageSize}, cb) {
    return this.find({name}).sort({'_id':-1}).skip(pageNum*pageSize).limit(pageSize).exec(cb);
  }

};

module.exports = VersionHistorySchema;
